<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var distributeCookies = function (cookies, k) {
      let res = Infinity;
      const allSelect = Array(k).fill(0);
      const dfs = (idx) => {
        if (idx === cookies.length) {
          const max = Math.max(...allSelect);
          res = Math.min(max, res);
          return;
        }
        for (let j = 0; j < k; j++) {
          allSelect[j] += cookies[idx];
          dfs(idx + 1);
          allSelect[j] -= cookies[idx];
        }
      };
      dfs(0);
      return res;
    };
    console.log(distributeCookies(cookies = [8, 15, 10, 20, 8], k = 2))
  </script>
</body>

</html>